package org.chenbingkang.other;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;

import java.util.Scanner;

public class EchoClient {
    static final boolean SSL = System.getProperty("ssl") != null;
    static final String HOST = System.getProperty("host", "119.84.240.43");
    static final int PORT = Integer.parseInt(System.getProperty("port", "7611"));
    static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));

    public static void main(String[] args) throws Exception {
        // Configure SSL.git
        final SslContext sslCtx;
        if (SSL) {
            sslCtx = SslContextBuilder.forClient()
                    .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
        } else {
            sslCtx = null;
        }

        // Configure the client.
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            if (sslCtx != null) {
                                p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                            }
                            //p.addLast(new LoggingHandler(LogLevel.INFO));
                            p.addLast(new EchoClientHandler());
                        }
                    });

            // Start the client.
            ChannelFuture f = b.connect(HOST, PORT).sync();
            Thread thread=new Thread(){
                @Override
                public void run() {
                    System.out.println("请进行输入：");
                    {
                      //  String s = "7e01020011000000043672025d436944695f303030303030303433363732517e";
                        String s="7e0100002f00000004428000090000000043694469004950552056332e300000000000000000000000000000343432383001b4a84131303435304600e17e";
                        byte[] buffer =   Hex.decodeHex(s.toCharArray());
                        ByteBuf byteBuf = Unpooled.buffer(buffer.length);
                        byteBuf.writeBytes(buffer);
                        f.channel().writeAndFlush(byteBuf);
                    }
                    Scanner scanner=new Scanner(System.in);
                    while (scanner.hasNext()){
                        String s = scanner.next();
                        byte[] buffer =   Hex.decodeHex(s.toCharArray());
                        ByteBuf byteBuf = Unpooled.buffer(buffer.length);
                        byteBuf.writeBytes(buffer);
                        f.channel().writeAndFlush(byteBuf);
                    }
                }
            };
            thread.start();
            // Wait until the connection is closed.
            f.channel().closeFuture().sync();
        } finally {
            // Shut down the event loop to terminate all threads.
            group.shutdownGracefully();
        }
    }
}
